from pygame.constants import MOUSEBUTTONDOWN
from lib.ZNode import ZNode

# self.onclicksound = _create_sound ( SRC [ 'sound' ] [ 'ok' ] )
from lib.res.fun import _change_size


class ZButton ( ZNode ) :
    def __init__ ( self , father , surface_in , surface_out , size = None ) :
        super ( ZButton , self ).__init__ ( )
        self.father = father
        self.image = self.default = surface_out
        self.over = surface_in
        self.rect = self.image.get_rect ( )
        self._pos = 0 , 0
        if size :
            self._size = size
            self.image = _change_size ( self.image , self._size )
            self.default = _change_size ( self.image , self._size )
            self.over = _change_size ( self.image , self._size )

    def draw ( self ) :
        if self._running :
            self.father.screen.blit ( self.image , self._pos )

    def isover ( self , mouse_pos , mouse_key ) :
        return True if self.rect.collidepoint ( mouse_pos ) else False

    def isout ( self ) :
        self.image = self.default

    def update ( self , event , mouse_pos , mouse_key ) :
        if self._running :
            if self.isover ( mouse_pos , mouse_key ) :
                self.image = self.over
            else :
                self.image = self.default
            if event.type == MOUSEBUTTONDOWN :
                self.isclick ( mouse_pos , mouse_key )

    def isclick ( self , mouse_pos , mouse_key ) :
        if self.isover ( mouse_pos , mouse_key ) :
            self._action ( )

    def _action ( self ) :
        pass
